<template>
  <div class="createPost-container">
    <ArticleForm
      :id="$route.params && $route.params.id"
      :is-edit="isEdit"
      :title="postForm.title"
      :content="postForm.content"
      :dialog-form-visible="dialogFormVisible"
      @closeFormDialog="closeFormDialog"
      @closeChildDialog="closeChildDialog(arguments)"
    />
    <!--    ref可以通过this.$refs.postForm获取到form表单的内容-->
    <el-form ref="postForm" :model="postForm" class="form-container">
      <!--当前页面的磁贴-->
      <sticky :z-index="10" :class-name="'sub-navbar '+postForm.status">
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submit">
          发布文章
        </el-button>
      </sticky>

      <div class="createPost-main-container">
        <el-row>

          <el-col :span="24">
            <el-form-item style="margin-bottom: 40px;" prop="title">
              <MDinput v-model="postForm.title" :maxlength="100" name="name" required>
                标题
              </MDinput>
            </el-form-item>

            <div class="postInfo-container">
              <el-row>
                <el-col :span="10">
                  <el-form-item label-width="120px" label="初次发布时间:" class="postInfo-container-item">
                    {{ postForm.display_time }}
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label-width="120px" label="最后修改时间:" class="postInfo-container-item">
                    {{ postForm.last_time }}
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>
        <el-form-item style="margin-bottom: 30px;">
          <Tinymce ref="editor" v-model="postForm.content" :height="400" />
        </el-form-item>
      </div>
    </el-form>
    <!--    <div v-html="postForm.content"/>-->
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
// import Upload from '@/components/Upload/SingleImage3'
import MDinput from '@/components/MDinput'

import Sticky from '@/components/Sticky' // 粘性header组件
import { fetchArticle } from '@/api/article'
import ArticleForm from '@/views/my/article/components/ArticleForm'
const defaultForm = {
  title: '', // 文章题目
  content: '', // 文章内容
  display_time: undefined, // 前台展示时间
  last_time: undefined,
  tags: []
}
export default {
  name: 'ArticleDetail',
  components: { ArticleForm, Tinymce, MDinput, Sticky },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
  /*  const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
        callback(new Error(rule.field + '为必传项'))
      } else {
        callback()
      }
    }*/

    return {
      dialogFormVisible: false,
      postForm: Object.assign({}, defaultForm),
      loading: false,
      tempRoute: {}
    }
  },

  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
    }

    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    // 点击dialog中取消按钮时调用
    closeFormDialog() {
      this.dialogFormVisible = false
    },
    // 点击dialog中发布按钮时调用
    closeChildDialog(data) {
      this.dialogFormVisible = false
      this.$message({
        type: data[0],
        message: data[1]
      })
    },
    fetchData(id) {
      fetchArticle(id).then(response => {
        const { article } = response.data
        this.postForm.title = article.blogTitle
        this.postForm.display_time = article.blogTime
        this.postForm.content = article.articleContent
        this.postForm.tags = article.tags
        this.postForm.last_time = article.lastTime
      }).catch(err => {
        console.log(err)
      })
    },
    submit() {
      if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
        this.$message({
          message: '请填写必要的标题和内容',
          type: 'warning'
        })
        return
      } else this.dialogFormVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
<style>
.el-tag + .el-tag  {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.popoverBtn  + .popoverBtn{
  margin-right: 20px;
  margin-bottom: 10px;
}
</style>
